package server;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Vector;

import shared.Vec2f;

public class Simulation {

	public Vector <Entity> m_entities;
	
	public Simulation()
	{
		m_entities=new Vector<Entity>();
		try {
			if (!Load())
			{
				Generate();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Generate();
		}
		
	}
	
	void Generate()
	{
		Entity entity=new Entity(new Vec2f(10,10), "planetzero");
		entity.WorldGenerator((short)16,(short) 16);
		//build one world
		m_entities.add(entity);
		
	}
	
	boolean Save() throws IOException //attempt to save the world as a file
	{
		FileOutputStream fstream;
		DataOutputStream dstream;
		//start writing to MEF file
		try {
			fstream=new FileOutputStream("save/MEF.sav");
			dstream=new DataOutputStream(fstream);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		//save entity count
		dstream.writeInt(m_entities.size());

		//save names of all entities
		for (int i=0;i<m_entities.size();i++)
		{
			dstream.writeInt(m_entities.get(i).m_id.length());
			byte []putter=m_entities.get(i).m_id.getBytes("US-ASCII");
			for (int j=0;j<m_entities.get(i).m_id.length();j++)
			{
				dstream.write(putter[j]);
			}

		}
		
		fstream.close();
		//for loop, save all entities
		for (int i=0;i<m_entities.size();i++)
		{
			m_entities.get(i).Save();
		}
		//each entity saves as its own file
		
		
		return false;
	}
	
	boolean Load() throws IOException //attempt to find files to load
	{
		//attempt to load
		FileInputStream fstream;
		DataInputStream dstream;
		try {
			
			fstream=new FileInputStream("save/MEF.sav");
			dstream=new DataInputStream(fstream);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		int size=dstream.readInt();
		String []strings=new String[size];
		for (int i=0;i<size;i++)
		{
			int s=dstream.readInt();
			byte []buffer=new byte[s];
			for (int j=0;j<s;j++)
			{
				buffer[j]=dstream.readByte();
			}
			strings[i]=new String(buffer);
		}
		for (int i=0;i<size;i++)
		{
			Entity entity=Entity.LoadEntity(strings[i]);
			if (entity!=null)
			{
				m_entities.add(entity);
			}
			else
			{
				return false;
			}
			
		}

		return false;
	}
	
	public void Start()
	{
		
	}
	
	public void End()
	{
		try {
			Save();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void Update(float dt)
	{
		
	}
}
